package com.sservice.web.controller.admin;

import com.sservice.common.Constants;
import com.sservice.common.SameUrlData;
import com.sservice.model.dao.OrderAdvanceReceiptMapper;
import com.sservice.model.vo.OrderAdvanceReceiptVo;
import com.sservice.service.service.IOrderAdvanceReceiptService;
import com.sservice.service.service.IOrderService;
import com.sservice.service.service.ISysLogService;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.model.entity.OrderAdvanceReceipt;
import com.sservice.urp.entity.User;
import com.sservice.web.log.Action;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

/**
 * 通过预收款单进行付款
 */
@Controller
@RequestMapping("/admin/orderAdvanceReceipt")
public class OrderAdvanceReceiptController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IOrderService depService;
    @Autowired
    private IOrderAdvanceReceiptService advanceReceiptService;
    @Autowired
    private OrderAdvanceReceiptMapper advanceReceiptMapper;
    @Autowired
    private ISysLogService sysLogService;

    /**
     * 通过预付款单进行订单付款   销售订单付款
     *
     * @param arg
     * @param session
     * @return
     */
    @SameUrlData
    @ResponseBody
    @RequestMapping(value = "/payByAdvanceReceipt", method = RequestMethod.POST)
    public BaseResult<Object> payByAdvanceReceipt(OrderAdvanceReceiptVo arg, HttpSession session, HttpServletRequest request) {
        User user = (User) session.getAttribute(Constants.USER);
        arg.setOperator(String.valueOf(user.getId()));

        Object obj = advanceReceiptService.payByAdvanceReceipt(arg);
        if (obj instanceof String) {

            return new BaseResult(false, obj.toString(), 403);
        } else {
            sysLogService.log("订单收款，收款金额【" + calculateTotal(arg.getPayCashDesc()) + "】", request, Integer.valueOf(arg.getOrderId()), Action.orderType);
            return new BaseResult(true, obj.toString(), 200);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/selectPayToOrderByOrderId/{orderId}/{type}", method = RequestMethod.POST)
    public BaseResult<Object> selectPayToOrderByOrderId(OrderAdvanceReceiptVo arg, HttpSession session, HttpServletRequest request) {
        return new ListResult(true, advanceReceiptMapper.selectPayToOrderByOrderId(arg));
    }

    /**
     * 撤销已收款
     *
     * @param arg
     * @param session
     * @param request
     * @return
     */
    @SameUrlData
    @ResponseBody
    @RequestMapping(value = "/rebackByAdvanceReceipt/{advanceReceiptId}/{orderId}", method = RequestMethod.POST)
    public BaseResult<Object> rebackByAdvanceReceipt(OrderAdvanceReceiptVo arg, HttpSession session, HttpServletRequest request) {
        User user = (User) session.getAttribute(Constants.USER);
        arg.setOperator(String.valueOf(user.getId()));
        arg.setType("0");
        Object obj = advanceReceiptService.rebackByOrderAdvanceReceipt(arg);
        if (obj instanceof String) {
            return new BaseResult(false, obj.toString(), 403);
        } else {
            sysLogService.log("撤销已收款", request, Integer.valueOf(arg.getOrderId()), Action.orderType);
            return new BaseResult(true, "", 200);
        }
    }

    public BigDecimal calculateTotal(String total) {
        String[] total_ = total.split(",");
        BigDecimal bd = BigDecimal.valueOf(0.0);
        for (int i = 0; i < total_.length; i++) {
            double d = Double.valueOf(total_[i]);
            bd = bd.add(BigDecimal.valueOf(d));
        }
        return bd;
    }

    /**
     * 付款单支付订单情况
     *
     * @param arg
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/payedByAdvance", method = RequestMethod.POST)
    public ListResult<OrderAdvanceReceiptVo> payedByAdvance(OrderAdvanceReceipt arg, HttpSession session) {
        List<OrderAdvanceReceiptVo> obj = null;
        if (arg.getType().equals("1")) {
            obj = advanceReceiptMapper.selectByAdvanceId1(arg);
        } else {
            obj = advanceReceiptMapper.selectByAdvanceId0(arg);
        }
        return new ListResult(true, obj);

    }

    /**
     * 根据预付/收款单查询付款订单
     *
     * @param arg
     * @param session
     * @return
     */
//    @ResponseBody
//    @RequestMapping(value = "/payedOrder", method = RequestMethod.POST)
//    public ListResult<OrderAdvanceReceiptVo> payedOrder(OrderAdvanceReceipt arg, HttpSession session) {
//        List<OrderAdvanceReceiptVo> obj = advanceReceiptService.selectByAdvanceId(arg);
//        return new ListResult(true,obj);
//
//    }
    @SameUrlData
    @ResponseBody
    @RequestMapping(value = "/payOrder", method = RequestMethod.POST)
    public BaseResult<Object> payOrder(OrderAdvanceReceiptVo arg, HttpSession session) {
        User user = (User) session.getAttribute(Constants.USER);
        arg.setOperator(String.valueOf(user.getId()));
        Object obj = advanceReceiptService.payOrder(arg);
        if (obj instanceof String) {
            return new BaseResult(false, obj.toString(), 403);
        } else {
            return new BaseResult(true, obj.toString(), 200);
        }
    }
}
